package com.example.imageandvideo.base

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.animation.AlphaInAnimation
import com.chad.library.adapter.base.animation.BaseAnimation
import com.example.imageandvideo.R

/**
 *
 * @Package: com.swit.hse.template
 * @ClassName: BaseRecyclerViewTemplate
 * @Author: szj
 * @CreateDate: 6/4/21 4:37 PM
 * @UpdateRemark: RecyclerView 模板方法模式
 * @Version: 1.0
 */
abstract class BaseRecyclerViewTemplate {

    private var adapter: BaseQuickAdapter<*, *>? = null

    open fun template(context: Context?, rel: RecyclerView) {

        //创建管理器
        rel.layoutManager = buildLayoutManager(context)

        //创建适配器
        adapter = buildAdapter()
        if (adapter != null) {
            //绑定适配器
            rel.adapter = adapter

            //设置动画
            adapter?.openLoadAnimation(BaseQuickAdapter.ALPHAIN)

            //动画是否重复执行
            adapter!!.isFirstOnly(isAnimationRepeat())
        }

        //空布局
        adapter?.emptyView = LayoutInflater.from(context).inflate(R.layout.no_layout, null, false)

        if (headView(context) != null) {
            adapter?.addHeaderView(headView(context))
        }

        if (bottomView(context) != null) {
            adapter?.addFooterView(bottomView(context))
        }
    }


    /**
     * 创建适配器
     */
    protected abstract fun buildAdapter(): BaseQuickAdapter<*, *>?

    /**
     * 创建管理器
     */
    open fun buildLayoutManager(context: Context?): RecyclerView.LayoutManager {
        return LinearLayoutManager(context)
    }

    /**
     * 设置渐变动画
     */
    fun getAnimation(): BaseAnimation = AlphaInAnimation()

    /**
     * 动画是否重复执行
     *
     * @return false重复执行  默认不重复执行
     */
    open fun isAnimationRepeat(): Boolean {
        return true
    }

    /**
     * 获取当前适配器
     */
    abstract fun getAdapter(): RecyclerView.Adapter<*>

    //顶部布局
    open fun headView(context:Context?): View? {
        return null
    }

    //底部布局
    open fun bottomView(context:Context?): View? {
        return null
    }
}